<?php
/**
 * 问题反馈模型
 * Created by PhpStorm.
 * User: zxj
 * Date: 17/5/1
 * Time: 上午9:49
 */

namespace App\Models;

class Response extends BaseModel
{
    protected $table = "response";


    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->multiWhere($where)->delete();
    }

    /**
     *插入数据
     */
    public function add($data)
    {
//        dd($data);
        return $this->insertGetId($data);
    }

    /**
     *更新数据
     */
    public function up($where, $data)
    {
        return $this->multiWhere($where)->update($data);
    }

    /**
     * 获取和
     */
    public function getSum($where, $field)
    {
        return $this->multiWhere($where)->sum($field);
    }

    /**
     * 字段递加
     */
    public function inc($where, $field, $n)
    {
        return $this->multiWhere($where)->increment($field, $n);
    }

    /**
     * 添加问题反馈
     * @param $request
     * @return array
     */
    public function AddResponse($request)
    {
        $data['user_id'] = $request['mid'];
        $data['ask_content'] = $request['ask_content'];
        $data['response_content'] = '';
        $data['created_at'] = date("Y-m-d", time());
        $data['updated_at'] = date("Y-m-d", time());
        $mid = $this->add($data);
        if (!$mid) {
            $res = ['state' => 0, 'msg' => '添加问题反馈失败'];
            return $res;
        }
        $res = ['state' => 1, 'msg' => '添加问题反馈成功'];
        return $res;
    }
    /**
     *显示反馈问题
     *
     *
     */
    public function getResponseList()
    {

        $where=[];
        $order['id'] = 'desc';
        $rows = $this->getList($where, '*', $order, 10);
        if ($rows) {
            $rows = $rows->toArray();
            $rows['pageNoList'] = getPageNoList($rows['last_page'], request('page', 1), 10);
        }
        $user = new User();
        foreach($rows['data'] as $key =>$value){
             $re['id'] = $value['user_id'];
             $user_info = $user->getUserById($re);
             $rows['data'][$key]['username'] = $user_info['data']['wx_name'];
        }
        return $rows;
    }


    /**
     * 回复
     * @param $request
     * @return array
     */
    public function reply($request){
        $id = $request['id'];
        $con = $request['con'];
        if(empty($con)){
            return ['state'=>0,'msg'=>"回复内容不能空"];
        }
        $data['response_content'] = $con;
        $data['status'] = 1;
        $where['id'] = $id;
        $res = $this->up($where,$data);
        if(!$res){
            return ['state'=>0,'msg'=>"回复失败"];
        }
        return ['state'=>1,'msg'=>"回复成功"];
    }



}